package components
{
	import UIFree.controls.uif_arrowbutton;
	import UIFree.controls.uif_maskedArea;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	import UIFree.uif_loader;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;

	public class pj_banners extends uif_ecomponent {
		private var _bannerArea: uif_maskedArea;
		private var _list: Array;
		private var _bsize: Array;
		private var _selectIndex: int;
		private var _curBanner: uif_loader;
		private var _lButton: uif_arrowbutton;
		private var _rButton: uif_arrowbutton;
		
		public function pj_banners(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			var inner: Rectangle = getInner();
			var bSize: Number = 22;
			_bannerArea = new uif_maskedArea(this, inner.x, inner.y, inner.width, inner.height - bSize);
			_curBanner	= new uif_loader(_bannerArea, '', doLoad);
			_curBanner.addEventListener(MouseEvent.ROLL_OUT, doOut);
			
			_lButton = new uif_arrowbutton(this, inner.x + (inner.width - bSize) / 2 - bSize, inner.y + inner.height - bSize, bSize, bSize, 4, 'button'); 
			_rButton = new uif_arrowbutton(this, inner.x + (inner.width - bSize) / 2 + bSize, inner.y + inner.height - bSize, bSize, bSize, 2, 'button');
			
			_lButton.addEvent(MouseEvent.CLICK, function(button: Object): void {
				selectIndex = (selectIndex - 1 >= 0)?(selectIndex - 1):(_list.length - 1);
			});

			_rButton.addEvent(MouseEvent.CLICK, function(button: Object): void {
				selectIndex = (selectIndex + 1) % _list.length;
			});
		}
		
		protected function doOut(e: MouseEvent): void {
			Mouse.cursor = MouseCursor.ARROW;
		}
		
		override public function dispose():void {
			_curBanner.removeEventListener(MouseEvent.ROLL_OUT, doOut);
			_curBanner.dispose();
			super.dispose();
		}
		
		protected function doLoad(e: Event): void {
			_curBanner.scaleX = 1;
			_curBanner.scaleY = 1;
			if (_bsize[selectIndex]) {
				_curBanner.scaleX = _bsize[selectIndex][0];
				_curBanner.scaleY = _bsize[selectIndex][1];
			}
			Mouse.show();
		}
		
		public function get list(): Array {
			return _list;
		}
		
		public function setList(a_list: Array, a_sizes: Array): void {
			_selectIndex = -1;
			_list = a_list;
			_bsize = a_sizes;
			if (_list.length > 0) selectIndex = Math.floor(Math.random() * _list.length);
		}
		
		public function get selectIndex(): int {
			return _selectIndex;
		}

		public function set selectIndex(a_index: int): void  {
			setSelectIndex(a_index);
		}
		
		protected function setSelectIndex(a_index: int): void {
			if (_selectIndex != a_index) {
				_selectIndex = a_index;
				_curBanner.unload();
				_curBanner.load(new URLRequest(_list[_selectIndex]));
			}
		}
	}
}